<?php
//占位
class class_pool
{
	var $vs = [];
	var $curr = [];
	
	function __construct($array)
	{
		foreach($array as $v)
		{
			$func = "init_{$v}";
			$this->{$func}();
		}
	}
	//---------------------------------------------------------------------------------------
	function on()
	{
		$this->curr[$GLOBALS['CID']] = [];
	}
	//---------------------------------------------------------------------------------------
	function off()
	{
		foreach($this->curr[$GLOBALS['CID']] as $type => $array)
		{
			foreach($array as $k => $v)
			{
				$this->vs[$type][$k]->put($v);
			}
		}
	}
//--------------------------------------------------------------------------------
	function get($type, $int)
	{
		$object = $this->vs[$type][$int]->get();
		$this->curr[$GLOBALS['CID']] = $object;
		return $object;
	}
//--------------------------------------------------------------------------------
	function init_mysql()
	{
		foreach($GLOBALS['MYSQL_CONFIG'] as $k => $v)
		{
			if(empty($this->vs['mysql']) || empty($this->vs['mysql'][$k]))
			{
				$host = $v['host'];
				$port = $v['port'];
				$port = $port ? $port : 3306;
				$user = $v['user'];
				$user = $user ? $user : '';
				$pwd = $v['pwd'];
				$pwd = $pwd ? $pwd : '';
				$dbname = $v['dbname'];
				
		    $pool = new Swoole\Database\PDOPool((new Swoole\Database\PDOConfig)
	        ->withHost($host)
	        ->withPort($port)
	        ->withDbName($dbname)
	        ->withCharset('utf8mb4')
	        ->withUsername($user)
	        ->withPassword($pwd)
		    );
				
				$this->vs['mysql'][$k] = $pool;
			}
		}
	}
//--------------------------------------------------------------------------------

	function init_redis()
	{
		foreach($GLOBALS['REDIS_CONFIG'] as $k => $v)
		{
			if(empty($this->vs['redis']) || empty($this->vs['redis'][$k]))
			{
				$host = $v['host'];
				$port = $v['port'];
				$port = $port ? $port : 6379;
				$pwd = $v['pwd'];
				$pwd = $pwd ? $pwd : '';
				$index = $v['index'];
				
		    $pool = new Swoole\Database\RedisPool((new Swoole\Database\RedisConfig)
		        ->withHost($host)
		        ->withPort($port)
		        ->withAuth($pwd)
		        ->withDbIndex($index)
		        ->withTimeout(1)
		    );
				
				$this->vs['redis'][$k] = $pool;
			}
		}
	}
//--------------------------------------------------------------------------------

	function init_mongo()
	{
		include_once($GLOBALS['DIR'].'/ext/mongo/index.php');
		
		foreach($GLOBALS['MONGO_CONFIG'] as $k => $v)
		{
			if(empty($this->vs['mongo']) || empty($this->vs['mongo'][$k]))
			{
				$host = $v['host'];
				$port = $v['port'];
				$port = $port ? $port : 6379;
				$user = $v['user'];
				$user = $user ? $user : '';
				$pwd = $v['pwd'];
				$pwd = $pwd ? $pwd : '';
				$dbname = $v['dbname'];
				
				$params = array(
					"username" => $user,
					"password" => $pwd,
				);
				
		    $conn = new Mongo("mongodb://{$host}:{$port}", $params);
				
				if(!$conn)
					return;
				
				$db = $conn->selectDB($dbname);
				$this->vs['mongo'][$k] = $db;
			}
		}
	}
}